package tech.aistar.corejava.day16;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

/**
 * @author success
 * @version 1.0
 * @description:本类用来演示:
 * @date 2019/8/5 0005
 */
@SuppressWarnings("all")
public class FileTest {
    public static void main(String[] args) {
        //listAllFilesWithSuffix("D:\\Program Files\\Java\\jdk1.8.0_66");
        //delAnyFolder("E:\\jdk1.8.0_66");

        try {
            copyFolder("E:\\jdk1.8.0_66","E:\\jdk1.8.0_66_copy");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 列出某个目录下的所有的文件或者目录
     * @param folder 目录
     */
    public static void listAllFiles(String folder){
        //构建一个File对象
        File file = new File(folder);

        File[] files = file.listFiles();

        for (File f:files){
            if(f.isFile()){
                System.out.println(f.getName());
            }else if(f.isDirectory()){
                System.out.println(f.getAbsolutePath());
                //递归调用自己
                listAllFiles(f.getAbsolutePath());
            }
        }
    }

    /**
     * 列出某个目录下的所有的文件或者目录
     * 找出所有的.html文件.
     * @param folder 目录
     */
    public static void listAllFilesWithSuffix(String folder){
        //构建一个File对象
        File file = new File(folder);

        File[] files = file.listFiles(new FileSuffixFilter(".html"));

        for (File f:files){
            if(f.isFile()){
                System.out.println(f.getName());
            }else if(f.isDirectory()){
                //System.out.println(f.getAbsolutePath());
                //递归调用自己
                listAllFilesWithSuffix(f.getAbsolutePath());
            }
        }
    }

    /**
     * 递归 - 核心 - 把握住最后一层
     * 删除任意目录[非空或者空]
     * @param pathname
     */
    public static void delAnyFolder(String pathname){
        File file = new File(pathname);

        File[] files = file.listFiles();

        for (File f : files) {
            if(f.isDirectory()){
                delAnyFolder(f.getAbsolutePath());
                f.delete();//只剩下目录
            }else if(f.isFile()){
                f.delete();
            }
           // f.delete();
        }
        file.delete();
    }

    /**
     * 复制目录
     * @param src
     * @param target
     */
    public static void copyFolder(String src,String target) throws IOException {
        File file = new File(src);

        if(!file.exists()){
            System.out.println("对不起!源文件不存在!");
            return;
        }
        File file_copy = new File(target);

            if(file.isFile()){//拷贝
//                //文件的拷贝操作
                //构建带缓存的文件字符输入流
                BufferedReader br = null;
                //输出流
                PrintWriter pw = null;

                //初始化
                try {
                    br = new BufferedReader(new FileReader(src));
                    pw = new PrintWriter(new FileWriter(target),true);
                    //定义一个变量,用来保存读取到的内容
                    String line = null;
                    //循环读取
                    while(true){
                        line = br.readLine();
                        if(null==line){
                            break;
                        }
                        pw.println(line);
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }else if(file.isDirectory()){//创建.
                if(!file_copy.exists()){
                    //创建空的目录
                    file_copy.mkdirs();
                }
                String[] names = file.list();
               // System.out.println(Arrays.toString(names));

                //递归调用自己
                for (String fileName : names) {
                    copyFolder(src+"\\"+fileName,target+"\\"+fileName);
                }
            }
        }
}
